package us.icitap.commandcenter;

import java.util.logging.Logger;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;

import us.icitap.commandcenter.business.AppUser;
import us.icitap.commandcenter.business.ejb.IAppStatelessBean;
import us.icitap.commandcenter.ui.configuration.CallReasonLayout;
import us.icitap.commandcenter.ui.configuration.DispatcherConfigLayout;
import us.icitap.commandcenter.ui.configuration.GradeLayout;
import us.icitap.commandcenter.ui.custom.CustButton;
import us.icitap.commandcenter.ui.dispatcher.DispatcherVerticalLayout;
import us.icitap.commandcenter.ui.operator.OperatorVerticalLayout;
import us.icitap.commandcenter.ui.supervisor.CallSearchLayout;
import us.icitap.commandcenter.ui.supervisor.SupervisorVerticalLayout;
import us.icitap.commandcenter.utils.HandleNotification;
import us.icitap.commandcenter.utils.TPUtil;

import us.icitap.timsportal.business.SimpleUser;
import us.icitap.timsportal.ejb.remote.PortalBeanRemote;
import us.icitap.timsportal.entity.PortalSession;
import us.icitap.timsportal.entity.User;
import us.icitap.utils.AppPdf;
import us.icitap.utils.Result;

import com.itextpdf.text.DocumentException;
import com.vaadin.annotations.PreserveOnRefresh;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.server.Resource;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServletRequest;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;


/**
 * Main UI class
 */
@Theme("commandcentertheme")
@SuppressWarnings("serial")
@Title("Salla Operative")
@PreserveOnRefresh
public class CommandCenterUI extends UI {

	
	OperatorVerticalLayout operatorLayout;
	DispatcherVerticalLayout dispatcherLayout;
	SupervisorVerticalLayout supervisorLayout;
	CallReasonLayout callReasonLayout;
	DispatcherConfigLayout dispatcherConfigLayout;
	GradeLayout gradeLayout;
	private CallSearchLayout callSearchLayout;
	
	
	public final static Logger LOG = Logger.getLogger("CommandCenter_LOG");
	
	
	final CCMenuBar menubar = new CCMenuBar(this);
	final Panel panel = new Panel();
	
	public static IAppStatelessBean appStatelessBean;
	
	private String loginMsg = "";
	public String returnURL;
	
	
	
	@Override
	protected void init(VaadinRequest request) {
		_init(request);
//		_devInit();
	}
	
	protected void _init(VaadinRequest request) {
		
		loadResources();

		VaadinServletRequest vaadinRequest = (VaadinServletRequest) request;
		HttpServletRequest httpRequest = vaadinRequest.getHttpServletRequest();
		TPUtil portalUtil = new TPUtil(httpRequest);
 		
		boolean isValid = false;
		if(portalUtil.checkRequestParams() && portalUtil.isValidPortalEntry()){
			isValid = portalUtil.loadUserOnSession();
		}
		if(!isValid){
			getUI().getPage().setLocation(portalUtil.getReturnURL());
			getSession().close();
		} else {
			this.returnURL = portalUtil.getReturnURL();
			loadUser((SimpleUser)httpRequest.getSession().getAttribute(TPUtil.USER));		
			loadUI();
		}
		
	}
	
	private void _devInit(){
		this.loadResources();
		this.loadDevUser();
		
		this.loadUI();
	}
	
	private void loadUser(SimpleUser portalUser) {
		Result<AppUser> result = appStatelessBean.login(portalUser);
		if(result.isSucceeded()){
			this.getUI().getSession().setAttribute(AppUser.class, result.getObject());
		}
		else{
			HandleNotification.showErrorOnScreen(result.getMessage());
		}
				
	}
	
	private void loadDevUser(){
		Result<AppUser> result = appStatelessBean.devLogin();
		System.out.println(result.getObject().getUsername());
		if(result.isSucceeded()){
			
			this.getUI().getSession().setAttribute(AppUser.class, result.getObject());
		}
		else{
			HandleNotification.showErrorOnScreen(result.getMessage());
		}
	}
	
	private void loadUI(){
		this.addStyleName("sfond");
		
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(false);
		setContent(layout);
		layout.addStyleName("head");
		Resource res = new ThemeResource("img/timsLogo.jpg");
		Image image = new Image(null, res);
		layout.addComponent(image);
		
		layout.setSizeUndefined();
		layout.setWidth("100%");
		
		CustButton closeBtn = new CustButton("TIMS");
		closeBtn.addClickListener(new Button.ClickListener() {			
			@Override
			public void buttonClick(ClickEvent event) {
				getUI().getPage().setLocation(returnURL);
				getSession().getSession().setMaxInactiveInterval(0);
			}
		});
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setStyleName("menuBackground");
		hl.setSizeFull();
		
		//closeBtn.setStyleName(Runo.BUTTON_LINK);
		hl.addComponent(closeBtn);
		hl.addComponent(menubar);
		hl.setExpandRatio(menubar, 1);
	
		layout.addComponent(hl);
		
		
		layout.addComponent(panel);
		
		panel.addStyleName("panelCaption");
		panel.addStyleName("cust-margin-4");
		
		
		panel.setWidth("99%");
		
		VerticalLayout vl = new VerticalLayout();
		vl.setSizeFull();
		vl.setHeight("600px");
		
		panel.setContent(vl);
		
		panel.getContent().addStyleName("logo");
		
		
		GridLayout footerVl = new GridLayout(2,1);
		
		footerVl.addStyleName("footer");
		footerVl.setWidth("99%");
		footerVl.setHeight("50px");
		footerVl.setMargin(true);
		
		Label l = new Label(this.getUser().getUsername());
		l.addStyleName("userLabel");
		footerVl.addComponent(l, 0, 0);
		footerVl.setComponentAlignment(l, Alignment.MIDDLE_LEFT);
		
		if(getUser().getCommandCenter() != null){
			Label ccName = new Label(getUser().getCommandCenter().getName());
			ccName.addStyleName("ccLabel");
			ccName.setSizeUndefined();
			footerVl.addComponent(ccName);
			footerVl.setComponentAlignment(ccName, Alignment.MIDDLE_RIGHT);
			
		}
		layout.addComponent(footerVl);
		
		this.addDetachListener(new DetachListener() {

			@Override
			public void detach(DetachEvent event) {
				System.out
						.println("CommandCenterUI.init(...).new DetachListener() {...}.detach()");

			}
		});
	}
	
	private void loadResources() {
		InitialContext init;
		try {
			init = new InitialContext();
			appStatelessBean = (IAppStatelessBean) init.lookup("java:global/CommandCenter/AppStatelessBean");

		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	
	private void resetLayout(){
		this.panel.setContent(null);
		this.operatorLayout = null;
		this.dispatcherLayout = null;
		this.supervisorLayout = null;
		this.callSearchLayout = null;
		this.callReasonLayout = null;
		this.dispatcherConfigLayout = null;
		this.gradeLayout = null;
	}
	
	public void loadDispatcherConfigLayout(){
		this.resetLayout();
		this.dispatcherConfigLayout = new DispatcherConfigLayout();
		panel.setContent(this.dispatcherConfigLayout);
		panel.setCaption("KONFIGURIMI");
	}
	public void loadOperatorLayout(){
		this.resetLayout();
		operatorLayout = new OperatorVerticalLayout();
		panel.setContent(operatorLayout);
		panel.setCaption("OPERATOR");
	}
	
	public void loadDispatcherLayout(){
		this.resetLayout();
		dispatcherLayout = new DispatcherVerticalLayout();
		panel.setContent(dispatcherLayout);
		panel.setCaption("SHPERNDARES");
	}

	public void loadSupervisorLayout(){
		this.resetLayout();
		supervisorLayout = new SupervisorVerticalLayout();
		panel.setContent(supervisorLayout);
		panel.setCaption("SUPERVIZOR");
	}


	public void loadCallReasonLayout(){
		this.resetLayout();
		callReasonLayout = new CallReasonLayout();
		panel.setContent(callReasonLayout);
		panel.setCaption("KONFIGURIMI");
	}
	
	public void loadGradeLayout(){
		this.resetLayout();
		gradeLayout = new GradeLayout();
		panel.setContent(gradeLayout);
		panel.setCaption("KONFIGURIMI");
	}
	
	public static AppUser getUser(){
		return (AppUser)CommandCenterUI.getCurrent().getSession().getAttribute(AppUser.class.getName());
	}


	public void loadSearchLayout() {
		this.resetLayout();
		callSearchLayout = new CallSearchLayout();
		panel.setContent(callSearchLayout);
		panel.setCaption("INFORMACION MBI TELEFONATAT");
		
	}
}